<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="nav-box flexWrap">
					<view class="nav-item flexWarpCenterColumn" :class="{active:currmentIndex==0}" @tap="changeNav(0)">
						<text>已领取({{num}})</text>
						<text class="nav-line"></text>
					</view>
					<view class="nav-item flexWarpCenterColumn" :class="{active:currmentIndex==1}" @tap="changeNav(1)">
						<text>已使用({{use_num}})</text>
						<text class="nav-line"></text>
					</view>
				</view>
				<view class="list-box">
					<view class="list-head flexWrap">
						<view style="width: 264upx;">用户名</view>
						<view style="width: 132upx;">有效期</view>
						<view v-if="currmentIndex==0">操作</view>
					</view>
					<view class="list-cont">
						<view class="list-item flexWrap" v-for="(item,index) in couponList" :key="index">
							<view style="width: 264upx;" class="flexWarpColumn">
								<text class="ellipsis">{{item.nickname}}</text>
								<text>{{item.mobile}}</text>
							</view>
							<view style="width: 190upx;">{{item.end_time}}</view>
							<view v-if="currmentIndex==0" @tap="showDel(index,item.id)">作废</view>
						</view>
						<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
						<view class="empty-box flexWarpCenterColumn" v-if="couponList.length==0">
							<image src="../../static/coupon/empty.png" class="empty-image"></image>
							<view class="empty-tips">这里空空如也！</view>
						</view>
					</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageOpc:0,
				currmentIndex:0,
				coupon_id:"",
				couponList:[],
				num:0,
				use_num:0,
				//下拉刷新
				page: 1,
				size:10,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
			}
		},
		onLoad(ops) {
			this.coupon_id=ops.couponId
			this.getList();
		},
		onReady(){
			setTimeout(() => {
				this.pageOpc=1
			}, 100)
		},
		// 触底刷新
		onReachBottom() {
			if (this.more) {
				this.getList()
			}
		},
		// 下拉刷新
		onPullDownRefresh: function() {
			this.freshList();
			this.$tools.loadIng()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		methods: {
			changeNav(index){
				this.currmentIndex=index
				this.freshList();
			},
			freshList() {
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.couponList = []
				this.getList();
			},
			getList(){
				let self=this
				this.$request.post(this.$api.userCoupon, {
					page:this.page++,
					num:this.size,
					store_id:uni.getStorageSync('store_id'),
					coupon_id:this.coupon_id,
					status:this.currmentIndex
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						let newArray =data.list
						console.log(newArray)
						this.num=data.count.num||0
						this.use_num=data.count.use_num||0
						if (newArray.length == 0 && self.page == 2) {
							self.showEmpty = true
							self.showLoadStatus = false
							self.more = false // 不能再次请求了
							return false;
						} else {
							self.showLoadStatus = true
							self.more = true
							self.showEmpty = false
							self.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							self.status = 'noMore'
							self.more = false
							self.showEmpty = false
						}
						self.couponList=self.couponList.concat(newArray);
					}
				})
			},
			showDel(index,id){
				let that=this
				uni.showModal({
					title:'提示',
					content:'是否作废?',
					success(res) {
						if(res.confirm){
							that.couponDel(index,id)
						}
					}
				})
			},
			couponDel(index,id){
				this.$request.post(this.$api.couponDel, {
					store_id:uni.getStorageSync('store_id'),
					id:id
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.$tools.showToast('作废成功');
						this.couponList.splice(index,1)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.nav-box{
		padding: 20upx 132upx;
		.nav-item{
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #82819B;
			.nav-line{
				width: 86rpx;
				height: 4rpx;
				margin-top: 10upx;
			}
		}
		.active{
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			.nav-line{
				background: #26BCFD;
			}
		}
	}
	.list-box{
		.list-head{
			height: 60rpx;
			background: #1D1C5C;
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #8C98E3;
			padding: 0 36upx;
		}
		.list-cont{
			padding: 0 36upx;
			.list-item{
				padding:32upx 0;
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				border-bottom: 2rpx solid #36355E;
			}
			.ellipsis{
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 1;
			}
		}
	}
	.empty-box{
		margin-bottom: 64upx;
		.empty-image{
			width: 400upx;
			height: 400upx;
		}
		.empty-tips{
			font-size: 24upx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 34upx;
			color: #FFFFFF;
		}
	}
</style>
